package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.CartItem;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.persistence.CartItemMapper;
import org.csu.mypetstore.api.persistence.ItemMapper;
import org.csu.mypetstore.api.persistence.ProductMapper;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("cartService")
public class CartServiceImpl implements CartService {

    @Autowired
    private CartItemMapper cartItemMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ProductMapper productMapper;

    //由商品界面直接添加到购物车
    @Override
    public CommonResponse<List<CartVO>> addCartItem(String username, String itemId) {
        QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("itemid", itemId);
        CartItem cartItem = cartItemMapper.selectOne(queryWrapper);
        if(cartItem == null)
        {
            cartItem = new CartItem();
            BigDecimal totalprice = itemMapper.selectById(itemId).getListPrice();
            cartItem.setUsername(username);
            cartItem.setItemId(itemId);
            cartItem.setTotalprice(totalprice);
            cartItem.setQuantity(1);
            cartItemMapper.insert(cartItem);
        }
        else
        {
            BigDecimal totalprice = cartItem.getTotalprice().add(itemMapper.selectById(itemId).getListPrice());
            cartItem.setTotalprice(totalprice);
            cartItem.setQuantity(cartItem.getQuantity() + 1);
            cartItemMapper.update(cartItem, queryWrapper);
        }
        return getCartItemsByUsername(username);
    }

    //在购物车界面修改数量
    @Override
    public CommonResponse<List<CartVO>> addCartItem(String username, String itemId, int plusQty) {
        QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("itemid", itemId);
        CartItem cartItem = cartItemMapper.selectOne(queryWrapper);

        BigDecimal newQty = new BigDecimal(plusQty);
        BigDecimal totalprice = itemMapper.selectById(itemId).getListPrice().multiply(newQty);
        cartItem.setTotalprice(totalprice);
        cartItem.setQuantity(plusQty);
        cartItemMapper.update(cartItem, queryWrapper);

        return getCartItemsByUsername(username);
    }

    //删除购物车中某个商品
    @Override
    public CommonResponse deleteCartItem(String username, String itemId) {
        QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("itemid", itemId);
        cartItemMapper.delete(queryWrapper);
        return getCartItemsByUsername(username);
    }

    //清空购物车
    @Override
    public CommonResponse<List<CartVO>> clear(String username)
    {
        QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        cartItemMapper.delete(queryWrapper);
        return getCartItemsByUsername(username);
    }

    //获取用户购物车的某一商品
    @Override
    public CommonResponse<CartVO> getCartItem(String username, int itemId) {
        QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("itemid", itemId);
        CartItem cartItem = cartItemMapper.selectOne(queryWrapper);
        if(cartItem == null)
        {
            return CommonResponse.createForSuccessMessage("购物车中无此商品");
        }
        Item item = itemMapper.selectById(cartItem.getItemId());
        Product product = productMapper.selectById(item.getProductId());
        CartVO cartVO = cartItemTOCartVO(cartItem, item, product);
        return CommonResponse.createForSuccess(cartVO);
    }

    //由username获取购物车
    @Override
    public CommonResponse<List<CartVO>> getCartItemsByUsername(String username) {
        QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        List<CartItem> cartItemList = cartItemMapper.selectList(queryWrapper);

        if(cartItemList.isEmpty())
        {
            return CommonResponse.createForSuccessMessage("购物车为空");
        }

        List<CartVO> cartVOList = new ArrayList<>();
        for(CartItem cartItem : cartItemList)
        {
            Item item = itemMapper.selectById(cartItem.getItemId());
            Product product = productMapper.selectById(item.getProductId());
            CartVO cartItemVO = cartItemTOCartVO(cartItem, item, product);
            cartVOList.add(cartItemVO);
        }
        return CommonResponse.createForSuccess(cartVOList);
    }

    //封装CartVO
    private CartVO cartItemTOCartVO(CartItem cartItem, Item item, Product product)
    {
        CartVO cartItemVO = new CartVO();
        cartItemVO.setItem(item);
        cartItemVO.setProduct(product);
        cartItemVO.setUsername(cartItem.getUsername());
        cartItemVO.setItemId(cartItem.getItemId());
        cartItemVO.setQuantity(cartItem.getQuantity());
        cartItemVO.setTotalprice(cartItem.getTotalprice());
        return cartItemVO;
    }
}
